<?php

namespace app\common\model;
use think\Model;
use app\common\validate\AuthGroup as AuthGroupValidate;
use app\common\validate\AuthGroupUpdate as AuthGroupValidateUpdate;
class AuthGroup extends Model
{

    public function add($data)
    {
        $validate=new AuthGroupValidate();
        if($validate->check($data)){
            $data['menu_auth']=implode(',',$data['menu_auth']);
            AuthGroup::create([
                'group_name'   =>  $data['group_name'],
                'status'  =>  $data['status'],
                'menu_auth'   =>  $data['menu_auth'],
                'remark' =>  $data['remark'],
                'create_time' => time()
            ]);
            return ['valid' => 1,'msg' => "添加角色成功"];
        }else{
            return ['valid' => 0,'msg' => $validate->getError()];
        }
    }


    public function edit($data){
        $validate=new AuthGroupValidateUpdate();
        if($validate->check($data)){
            $data['menu_auth'] = implode(',',$data['menu_auth']);
            AuthGroup::where('id', $data['group_id'])
                ->update([
                    'group_name'   =>  $data['group_name'],
                    'status'  =>  $data['status'],
                    'remark' => $data['remark'],
                    'menu_auth'   =>  $data['menu_auth'],
                    'update_time' =>time()
                ]);
            return ['valid' => 1,'msg' => "更新数据成功"];
        }else{
            return ['valid' => 0,'msg' => $validate->getError()];
        }
    }

    public function status_edit($data){
        $group = AuthGroup::where('id=' . $data['id'])->find();
        $status = $group['status'];
        if ($status == 1) {
            $s_data = AuthGroup::where('id', $data['id'])
                ->update(array('status'=>0));
            if ($s_data){
                return ['valid' => 1,'msg' => "成功"];
            }else{
                return ['valid' => 0,'msg' => "失败"];
            }
        }
        if ($status !== 1 ) {
            $s_data = AuthGroup::where('id', $data['id'])
                ->update(array('status'=>1));
            if ($s_data){
                return ['valid' => 1,'msg' => "成功"];
            }else{
                return ['valid' => 0,'msg' => "失败"];
            }
        }

    }

}
